/*
 * editAdmin.java
 *
 * Created on __DATE__, __TIME__
 */

package hotel.view;

import hotel.dao.BianJiDao;
import hotel.util.DbUtil;

import java.sql.Connection;

import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class editAdmin extends javax.swing.JInternalFrame {
	DbUtil dbUtil = new DbUtil();
	BianJiDao editModulDao = new BianJiDao();
	public editAdmin() {
		initComponents();
	}

	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();

		jLabel1.setFont(new Font("宋体", Font.PLAIN, 22));
		jTextField1 = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
	
		jLabel2.setFont(new Font("宋体", Font.PLAIN, 22));
		password = new javax.swing.JPasswordField();
		jLabel3 = new javax.swing.JLabel();
		jLabel3.setFont(new Font("宋体", Font.PLAIN, 22));
	
		repassword = new javax.swing.JPasswordField();
		jButton1 = new javax.swing.JButton();
	
		jButton1.setFont(new Font("宋体", Font.PLAIN, 22));
		jLabel4 = new javax.swing.JLabel();
		jLabel4.setFont(new Font("宋体", Font.PLAIN, 22));
	
		oldpassword = new javax.swing.JPasswordField();

		setClosable(true);
		setResizable(true);

		jLabel1.setText("用户名");

		jTextField1.setText("admin");
		jTextField1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jTextField1ActionPerformed(evt);
			}
		});

		jLabel2.setText("新密码");

		password.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				passwordActionPerformed(evt);
			}
		});

		jLabel3.setText("确认密码");

		jButton1.setText("修改");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jLabel4.setText("旧密码");

		GroupLayout layout = new GroupLayout(
						getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(198)
					.addComponent(jButton1)
					.addContainerGap(274, Short.MAX_VALUE))
				.addGroup(layout.createSequentialGroup()
					.addGap(74)
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addComponent(jLabel3)
							.addGap(39)
							.addComponent(repassword, 0, 0, Short.MAX_VALUE))
						.addGroup(layout.createSequentialGroup()
							.addComponent(jLabel2)
							.addGap(61)
							.addComponent(password, 0, 0, Short.MAX_VALUE))
						.addGroup(layout.createSequentialGroup()
							.addGroup(layout.createParallelGroup(Alignment.LEADING)
								.addComponent(jLabel1)
								.addComponent(jLabel4))
							.addGap(61)
							.addGroup(layout.createParallelGroup(Alignment.LEADING)
								.addComponent(oldpassword, 0, 0, Short.MAX_VALUE)
								.addComponent(jTextField1, GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))))
					.addContainerGap(134, GroupLayout.PREFERRED_SIZE))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGap(104)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel1)
						.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(34)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel4)
						.addComponent(oldpassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jLabel2)
						.addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(jLabel3)
						.addComponent(repassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(50)
					.addComponent(jButton1)
					.addGap(55))
		);
		getContentPane().setLayout(layout);

		pack();
	}
	private void passwordActionPerformed(java.awt.event.ActionEvent evt) {
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		Connection con = null;
		String password = new String(this.password.getPassword());
		String repassword = new String(this.repassword.getPassword());
		String oldpassword = new String(this.oldpassword.getPassword());
		try {
			con = dbUtil.getCon();
			String oldpwd = editModulDao.oldPassword(con);
			if(!oldpassword.equals(oldpwd)){
				JOptionPane.showMessageDialog(null, "旧密码不符");
				return;
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if (password == null || "".equals(password)) {
			JOptionPane.showMessageDialog(null, "请输入密码");
			return;
		}
		if (!password.equals(repassword)) {
			JOptionPane.showMessageDialog(null, "两次输入密码不一致");
			return;
		}
		//修改数据库操作
		try {
			con = dbUtil.getCon();
			int result = editModulDao.updateAdmin(con, password);
			if (result != 0) {
				JOptionPane.showMessageDialog(null, "修改成功");
				this.password.setText("");
			} else {
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);//关闭数据库连接
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
	}


	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JPasswordField oldpassword;
	private javax.swing.JPasswordField password;
	private javax.swing.JPasswordField repassword;


}